package com.beiding.my.mql;

import com.beiding.my.bemapping.BeTypeMapper;
import org.springframework.stereotype.Component;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class SelectExecutorFactory extends ExecutorFactory {

    @Override
    MQLExecutor createExecutor(String sql, BeTypeMapper beTypeMapper) {

        MQLExecutor mqlExecutor;
        if (beTypeMapper == null) {
            mqlExecutor = new MQLExecutor() {
                @Override
                public Object queryOne(Object... params) {
                    Query query = nativeSql.createQuery(sql);
                    //参数赋值
                    if (params.length > 0) {
                        for (int i = 0; i < params.length; i++) {
                            query.setParameter("p" + (i + 1), params[i]);
                        }
                    }
                    return query.getSingleResult();
                }

                @Override
                public List queryList(Object... params) {
                    Query query = nativeSql.createQuery(sql);
                    //参数赋值
                    if (params.length > 0) {
                        for (int i = 0; i < params.length; i++) {
                            query.setParameter("p" + (i + 1), params[i]);
                        }
                    }
                    return query.getResultList();
                }

            };
        } else {
            mqlExecutor = new MQLExecutor() {
                @Override
                public Object queryOne(Object... params) {
                    Query query = nativeSql.createQuery(sql);

                    //参数赋值
                    if (params.length > 0) {
                        for (int i = 0; i < params.length; i++) {
                            query.setParameter("p" + (i + 1), params[i]);
                        }
                    }
                    Map<String, Object> singleResult = (Map<String, Object>) query.getSingleResult();
                    return beTypeMapper.newBeByAttributeId(singleResult);
                }



                @Override
                public List queryList(Object... params) {
                    Query query = nativeSql.createQuery(sql);
                    //参数赋值
                    if (params.length > 0) {
                        for (int i = 0; i < params.length; i++) {
                            query.setParameter("p" + (i + 1), params[i]);
                        }
                    }
                    List resultList = query.getResultList();

                    List r = new ArrayList();

                    for (Object o : resultList) {
                        Map<String, Object> singleResult = (Map<String, Object>) o;
                        r.add(beTypeMapper.newBeByAttributeId(singleResult));
                    }

                    return r;
                }

            };
        }

        return mqlExecutor;

    }

    @Override
    String name() {
        return "select";
    }
}
